/**
 * 
 */
package com.hawkfly.uploader;

import java.io.File;
import java.util.Random;

import com.oreilly.servlet.multipart.FileRenamePolicy;

/**
 * @author hawkfly
 *
 */
public class JBSFRenamePolicy implements FileRenamePolicy
{

    /* (non-Javadoc)
     * @see com.oreilly.servlet.multipart.FileRenamePolicy#rename(java.io.File)
     */
    public File rename(File file)
    {
        String prepath = file.getAbsolutePath().substring(0,file.getAbsolutePath().lastIndexOf("\\"));
        String filename = getName(file.getName());
        String url = prepath + "\\" + filename;
        return new File(url);
    }
    
    /**
     * 依据原始文件名生成新文件名
     * @return
     */
    private String getName(String fileName) {
        Random random = new Random();
        return "" + random.nextInt(10000)
                + System.currentTimeMillis() + this.getFileExt(fileName);
    }

    /**
     * 获取文件扩展名
     * 
     * @return string
     */
    private String getFileExt(String fileName) {
        return fileName.substring(fileName.lastIndexOf("."));
    }
}

